<?php

include_once './app/view/webView.php' ;
include_once './app/model/userModel.php' ;

class adminController extends Controller
{
	
	public function __construct ( )
	{
		
		parent::__construct ( ) ;
		
		$this->filter['before'] = array ( 'verif_connection' => array ( 'user', 'account', 'type' ) ) ;
		
	}
	
	public function verif_connection ( )
	{
		
		if ( ! isset ( $this->session['admin'] ) )
			System::redirectTo ( ) ;
		else
			$this->data['admin'] = true ;
		
	}
	
	public function home ( )
	{
		
		$validator = new KValidator ( 'admin_form', KValidator::POST ) ;
		
		$sent = $validator->sent ( ) ;
		$valid = $validator->valid ( ) ;
		
		if ( isset ( $this->session['admin'] ) )
		{
			
			unset ( $validator, $sent, $valid ) ;
			
			$users = userModel::getAll ( ) ;
				
			$this->data['users'] = array ( 'total' => 1, 'page' => 1, 'records' => count ( $users ), 'rows' => array ( ) ) ;
				
			if ( $users != null )
				foreach ( $users as $u )
				{
						
					$row = array ( 'id' => $u->getId(), 'cell' => array ( $u->getId(), $u->getLastName ( ) . ' ' . $u->getFirstName ( ), $u->getLogin(), $u->getPassword() ) ) ;
					$this->data['users']['rows'][] = $row ;
						
				}
				
			unset ( $users ) ;
			
			$types = typeModel::getAll () ;
			
			$this->data['types'] = array ( 'total' => 1, 'page' => 1, 'records' => count ( $types ), 'rows' => array ( ) ) ;
			
			if ( $types != null )
				foreach ( $types as $t )
				{
			
					$row = array ( 'id' => $t->getId(), 'cell' => array ( $t->getId(), $t->getEntitled(), ($t->getCeiling()==null) ? 'Aucun' : number_format ( $t->getCeiling(), 2, ',', ' ' ) . ' €', ($t->getWithdraw()) ? 'Oui' : 'Non', ($t->getOverdraft() == null ) ? 'Sans limite' : number_format ( $t->getOverdraft(), 2, ',', ' ' ) . ' €' ));
					$this->data['types']['rows'][] = $row ;
			
				}
			
			unset ( $users ) ;
				
			$this->data['title'] = 'Administration' ;
			$this->data['users'] = str_replace( '"', '\"', json_encode($this->data['users']) ) ;
			$this->data['types'] = str_replace( '"', '\"', json_encode($this->data['types']) ) ;
			$this->data['admin'] = true ;
			$this->render ( 'webView', 'admin_home', 'admin_layout' ) ;
			
		}
		elseif ( $sent && $valid )
		{
			
			if ( sha1 ( $validator->getValue('password') ) == file_get_contents( 'config/admin_pass.txt' ) )
			{
				
				$this->data['text'] = 1 ;
				
				$this->session['admin'] = true ;
				
			}
			else
				$this->data['text'] = 0 ;
			
			unset ( $validator, $sent, $valid ) ;
			
			$this->data['admin'] = $this->session['admin'] ;
			$this->render ( 'webView', 'admin_connection_return' ) ;
			
		}
		else
		{
			
			unset ( $validator, $sent, $valid ) ;
			
			$this->data['title'] = 'Administration' ;
			$this->data['admin'] = false ;
			$this->render ( 'webView', 'admin_form', 'admin_layout' ) ;
			
		}
		
	}
	
	public function user ( )
	{
		
		$validator = new KValidator ( 'add_user_form', KValidator::POST ) ;
		
		$sent = $validator->sent ( ) ;
		$valid = $validator->valid ( ) ;
		
		$validator2 = new KValidator ( 'rm_user_form', KValidator::POST ) ;
		
		$sent2 = $validator2->sent ( ) ;
		$valid2 = $validator2->valid ( ) ;
		
		$validator3 = new KValidator( 'open_account_form', KValidator::POST ) ;
		
		$sent3 = $validator3->sent ( ) ;
		$valid3 = $validator3->valid ( ) ;
		
		if ( $sent && $valid )
		{
			
			if ( userModel::getByLogin($validator->getValue('login')) == null )
			{
				
				$u = new userModel();
				
				$u->setLogin($validator->getValue('login'));
				$u->setFirstName($validator->getValue('firstname'));
				$u->setLastName($validator->getValue('lastname'));
				
				$pass = '' ;
				
				for ( $i = 0 ; $i < 6 ; $i ++ )
				{
					
					$pass .= strval(rand(0,9));
					
				}
				
				$u->setPassword($pass);
				
				$u->save();
				
				$this->data['text'] = 1 ;
				
			}
			else
				$this->data['text'] = 0 ;
			
			$this->render ( 'webView', 'admin_add_user_return' ) ;
			
		}
		elseif ( $sent2 && $valid2 )
		{
			
			$u = userModel::getById( $validator2->getValue('id'));
			
			if ( $u != null )
			{
				
				if ( sha1 ( $validator2->getValue('pass') ) == file_get_contents( 'config/admin_pass.txt' ) )
				{
					
					$u->delete();
					$this->data['text'] = 1 ;
					
				}
				else
					$this->data['text'] = 0 ;
				
				$this->render ( 'webView', 'admin_rm_user_return' ) ;
				
			}
			
		}
		elseif ( $sent3 && $valid3 )
		{
			
			$acc = accountModel::getByRIB($validator3->getValue('cashdeskcode'), $validator3->getValue('accountnumber'), $validator3->getValue('ribkey'));
			$type = typeModel::getById($validator3->getValue('type'));
			
			if ( $acc == null && $type != null )
			{
				
				$acc = new accountModel();
				
				$acc->setOwner($validator3->getValue('id'));
				$acc->setCreationDate( date ( 'Y-m-d' ) );
				$acc->setgetCashDeskCode($validator3->getValue('cashdeskcode'));
				$acc->setAccountNumber($validator3->getValue('accountnumber'));
				$acc->setRIBKey($validator3->getValue('ribkey'));
				$acc->setType($validator3->getValue('type'));
				
				$acc->save();
				
				$this->data['text'] = 1 ;
				
			}
			else
				$this->data['text'] = 0 ;
			
			$this->render ( 'webView', 'admin_add_account_return' ) ;
			
		}
		elseif ( isset ( $this->params['id'] ) )
		{
			
			$u = userModel::getById( $this->params['id'] ) ;
			
			if ( $u == null )
				System::redirectTo('admin');
			
			$accounts = accountModel::getByOwner( $this->params['id'] ) ;
			
			$this->data['accounts'] = array ( 'total' => 1, 'page' => 1, 'records' => count ( $accounts ), 'rows' => array ( ) ) ;
				
			if ( $accounts != null )
				foreach ( $accounts as $acc )
				{
					
					$type = typeModel::getById( $acc->getType () ) ;
					
					$row = array ( 'id' => $acc->getId(), 'cell' => array ( $acc->getId(), $acc->getAccountNumber(), $type->getEntitled(), number_format ( $acc->getAmount(), 2, ',', ' ' ) . ' €' ));
					$this->data['accounts']['rows'][] = $row ;
						
				}
					
			unset ( $accounts ) ;
			
			$this->data['type'] = array ( ) ;
			
			$types = typeModel::getAll();
			
			foreach ( $types as $t )
			{
				
				$this->data['type'][] = array ( 'id' => $t->getId(), 'entitled' => $t->getEntitled());
				
			}
			
			$this->data['title'] = 'Information d\'un client' ;
			$this->data['id'] = $this->params['id'] ;
			$this->data['lastname'] = $u->getLastName();
			$this->data['firstname'] = $u->getFirstName();
			$this->data['login'] = $u->getLogin();
			$this->data['pass'] = $u->getPassword();
			$this->data['accounts'] = str_replace( '"', '\"', json_encode($this->data['accounts']) ) ;
			$this->render ( 'webView', 'admin_user_view', 'admin_layout' ) ;
			
		}
		else
			System::redirectTo('admin');
			
	}
	
	public function account ( )
	{
		
		if ( ! isset ( $this->params['id'] ) )
			System::redirectTo( 'admin' ) ;
		
		$acc = accountModel::getById($this->params['id']);
		
		if ( $acc == null )
			System::redirectTo('admin');
		
		$validator = new KValidator ( 'rm_account_form', KValidator::POST );
		
		$sent = $validator->sent();
		$valid = $validator->valid();
		
		if ( $sent && $valid )
		{
				
			if ( sha1 ( $validator->getValue('pass') ) == file_get_contents( 'config/admin_pass.txt' ) )
			{
					
				$acc->delete();
				$this->data['text'] = 1 ;
					
			}
			else
				$this->data['text'] = 0 ;
			
			$this->render ( 'webView', 'admin_rm_account_return' ) ;
			
		}
		elseif ( isset ( $this->params['page'] ) )
		{
			
			$trs = $acc->getTransactions ( ) ;
			
			$res_trs = array ( ) ;
			
			$start = count ( $trs ) - ( $this->params['page'] - 1 ) * $this->params['rows'] - 1 ;
			$end = $start - $this->params['rows'] + 1 ;
			if ( $end < 0 ) $end = 0 ;
			
			for ( $i = $start ; $i >= $end ; $i -- )
			{
					
				$res_trs[] = array (
						'id' => $trs[$i]->getId ( ),
						'cell' => array (
								$trs[$i]->getDate ( ),
								$trs[$i]->getEntitled ( ),
								number_format ( $trs[$i]->getAmount ( ), 2, ',', ' ' ) . ' €'
						)
				) ;
					
			}
			
			$res = array (
					'total' => ceil ( count ( $trs ) / intval ( $this->params['rows'] ) ),
					'page' => intval ( $this->params['page'] ),
					'records' => count ( $trs ),
					'rows' => $res_trs
			) ;
			
			echo json_encode ( $res ) ; exit ;
			
		}
		else
		{
			
			$client = userModel::getById($acc->getOwner());
			
			$this->data['id'] = $acc->getId();
			$this->data['cli_id'] = $acc->getOwner();
			$this->data['owner'] = $client->getLastName() . ' ' . $client->getFirstName() ;
			$this->data['creation_date'] = $acc->getCreationDate();
			$this->data['accountnumber'] = $acc->getAccountNumber();
			$this->data['cashdeskcode'] = $acc->getCashDeskCode();
			$this->data['ribkey'] = $acc->getRIBKey();
			$this->data['amount'] = $acc->getAmount();
			
			$type = typeModel::getById($acc->getType());
			
			$this->data['entitled'] = $type->getEntitled ( ) ;
			$this->data['ceiling'] = ( $type->getCeiling ( ) ) ? number_format ( $type->getCeiling ( ), 2, ',', ' ' ) . ' €' : 'Aucun' ;
			$this->data['withdraw'] = $type->getWithdraw ( ) ;
			$this->data['overdraft'] = ( $type->getOverdraft ( ) != null ) ? number_format ( $type->getOverdraft ( ), 2, ',', ' ' ) . ' €' : 'Aucune limite' ;
			
			$this->data['title'] = 'Détail d\'un compte';
			
			$this->render ( 'webView', 'admin_account_view', 'admin_layout' ) ;
			
		}
		
	}
	
	public function type ( )
	{
		
		$validator = new KValidator ( 'add_type_form', KValidator::POST ) ;
		
		$sent = $validator->sent ( ) ;
		$valid = $validator->valid ( ) ;
		
		if ( $sent && $valid )
		{
			
			$t = new typeModel();
			
			$t->setEntitled( $validator->getValue ( 'entitled' ) ) ;
			$t->setCeiling( ( $validator->getValue( 'ceiling' ) == '' ) ? null : floatval( $validator->getValue( 'ceiling' ) ) ) ;
			$t->setWithdraw( ( $validator->getValue('withdraw') == '1' ) ? 1 : 0 ) ;
			$t->setOverdraft( ( $validator->getValue( 'overdraft' ) == '' ) ? null : intval ( $validator->getValue( 'overdraft' ) ) ) ;
			
			$t->save();
			
			$this->data['text'] = 1 ;
				
			$this->render ( 'webView', 'admin_add_type_return' ) ;
				
		}
		else
			System::redirectTo('admin');
		
	}
	
}

?>